#!/bin/bash

date=$(date +%y%m%d)
top_name=$1
ftype="OASIS"
run_path="/projects.SE032/cascade/workspace/xwqiu/PV/check_dm"

##### rule
drc_rule="${run_path}/rule/CLN6FF_13M_1X1Xa1Ya5Y2Yy2Z_001.11a.encrypt_forIP"
dum_rule="${run_path}/rule/Dummy_CalibreYE_6nm.11a.forBlock"

##### check input file
if echo ${top_name} | grep -q "oasis"; then
    echo "this is OASIS file path!!"
    oasis_file=${top_name}
    ftype="OASIS"
elif echo ${top_name} | grep -q "gds"; then
    echo "this is GDSII file path!!"
    oasis_file=${top_name}
    ftype="GDSII"
else
    echo "this is top name!!"
    oasis_file="/projects.SE032/cascade/workspace/xwqiu/${top_name}/post.${date}/dataout/${top_name}.strm.${date}.oasis"
fi

top_name=`exec drvTopcell ${oasis_file}`

##### check output path
if [[ -d "${run_path}/run_DRC" ]]; then
	echo "${run_path}/run_DRC is exists."
else
	echo "${run_path}/run_DRC not exist."
        mkdir run_DRC
fi

if [[ -d "${run_path}/run_DUM" ]]; then
	echo "${run_path}/run_DUM is exists."
else
	echo "${run_path}/run_DUM not exist."
        mkdir run_DUM
fi

##### run dummy rule
cd ${run_path}
cd ${run_path}/run_DUM

out_dum_file="${run_path}/run_DUM/F11a_${top_name}.gds"
echo "LAYOUT SYSTEM ${ftype}" > ./DRC_DUM.cal
echo "LAYOUT PATH '${oasis_file}'" >> ./DRC_DUM.cal
echo "LAYOUT PRIMARY '${top_name}'" >> ./DRC_DUM.cal
echo "INCLUDE ${dum_rule}" >> ./DRC_DUM.cal

calibre -64 -drc -hier -hyper -turbo 32 ./DRC_DUM.cal | tee DRC_DUM.log

mv ./FEOL.gds ${out_dum_file}

##### run DRC rule
cd ${run_path}
cd ${run_path}/run_DRC

out_drc_file="${run_path}/run_DRC/${top_name}.gds"
exc="'/users/lshi/flow/pv/merge.tcl ${oasis_file} ${top_name} ${out_drc_file} F11a_${top_name} ${out_dum_file}'"

eval calibredrv ${exc} | tee mergeGDS.log
echo ${exc} >> mergeGDS.log

echo "LAYOUT SYSTEM OASIS" > ./DRC.cal
echo "LAYOUT PATH '${out_drc_file}'" >> ./DRC.cal
echo "LAYOUT PRIMARY ${top_name}" >> ./DRC.cal
echo "DRC MAXIMUM RESULTS ALL" >> ./DRC.cal
echo "INCLUDE ${drc_rule}" >> ./DRC.cal

/apps/mentor/Calibre_2019.3_25.15/aoi_cal_2019.3_25.15/bin/calibre -drc -hier -hyper -turbo 32 ./DRC.cal | tee DRC.log

cd ${run_path}/run_DRC
rep_drc
cd ${run_path}